package cn.sinata.wanglin.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.xilada.xldutils.utils.Utils;

import cn.sinata.wanglin.db.DBHelper;
import cn.sinata.wanglin.db.DbUtils;
import cn.sinata.wanglin.model.Group;

/**
 *
 * Created by liaoxiang on 17/1/3.
 */

public class GroupDao {

    public static final String COLUMN_NAME_GROUP_ID = "groupID";
    public static final String COLUMN_NAME_GROUP_ACCOUNT = "groupAccount";
    public static final String COLUMN_NAME_GROUP_NAME = "groupName";
    public static final String COLUMN_NAME_GROUP_IMG = "groupImg";
    public static final String COLUMN_NAME_GROUP_USER_NICK = "groupNickName";
    public static final String COLUMN_NAME_GROUP_MSG_STATUS = "msgStatus";

    private static DBHelper dbHelper;
    private GroupDao(){}
    public static GroupDao getInstance(Context context){
        dbHelper = DBHelper.getInstance(context);
        return GroupDao.Singleton.INSTANCE;
    }

    private static final class Singleton {
        private final static GroupDao INSTANCE= new GroupDao();
    }

    public void saveGroup(String userId,Group group){
        Utils.systemErr("-->"+group);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db != null && db.isOpen()) {
            if (group == null || TextUtils.isEmpty(group.getGroupAccount())) {
                return;
            }
            String tableName = getTableName(userId,group.getGroupAccount());
            if (!DbUtils.isTableExist(db,tableName)) {
                db.execSQL(getCreateTableSQLString(tableName));
            }
            ContentValues values = new ContentValues();
            if(group.getGroupName() != null) {
                values.put(COLUMN_NAME_GROUP_NAME, group.getGroupName());
            }
            if (group.getGroupImg()!= null) {
                values.put(COLUMN_NAME_GROUP_IMG, group.getGroupImg());
            }
            values.put(COLUMN_NAME_GROUP_ID,group.getId());
            values.put(COLUMN_NAME_GROUP_ACCOUNT,group.getGroupAccount());
            values.put(COLUMN_NAME_GROUP_MSG_STATUS,group.getMsgStatus());
            values.put(COLUMN_NAME_GROUP_USER_NICK,group.getOtherNickName());
            db.replace(tableName, null, values);
        }
    }

    public Group getGroup(String userId,String groupAccount){
        Group group = new Group();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db != null && db.isOpen()) {
            String tableName= getTableName(userId,groupAccount);
            if (DbUtils.isTableExist(db, tableName)) {
                Cursor cursor = db.rawQuery("select * from "+tableName+" where "+COLUMN_NAME_GROUP_ACCOUNT+" = ? ",new String[]{groupAccount});
                if (cursor.moveToFirst()) {
                    String groupId = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_ID));
                    String groupName = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_NAME));
                    String account = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_ACCOUNT));
                    String img = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_IMG));
                    int msgStatus = cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_GROUP_MSG_STATUS));
                    String userNick = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_USER_NICK));
                    group.setId(groupId);
                    group.setGroupAccount(account);
                    group.setGroupImg(img);
                    group.setGroupName(groupName);
                    group.setMsgStatus(msgStatus);
                    group.setGroupNickName(userNick);
                    cursor.close();
                    return group;
                }
            }
        }
        return group;
    }

    private String getTableName(String userId,String groupAccount){
        return "group_"+userId+"_"+groupAccount;
    }

    private String getCreateTableSQLString(String tableName){
        String sql=null;
        if (TextUtils.isEmpty(tableName)) {
            return sql;
        }
        sql="CREATE TABLE "
                + tableName.trim() + "  ("
                + COLUMN_NAME_GROUP_ACCOUNT +" TEXT PRIMARY KEY, "
                + COLUMN_NAME_GROUP_ID +" TEXT, "
                + COLUMN_NAME_GROUP_IMG +" TEXT, "
                + COLUMN_NAME_GROUP_USER_NICK +" TEXT, "
                + COLUMN_NAME_GROUP_MSG_STATUS +" INTEGER, "
                + COLUMN_NAME_GROUP_NAME +" TEXT);";
        return sql;
    }

}
